package org.fxcontainer.validation
{
	import mx.controls.TextInput;
	import mx.core.UIComponent;
	import mx.effects.Effect;
	import mx.effects.Glow;
	import mx.events.ValidationResultEvent;
	import mx.styles.StyleManager;
	
	/**
	 * Validation errors visualizer. 
	 * @author Izzet_Mustafayev
	 */
	public class ValidationVisualizer implements IValidationVisualizer
	{
		/**
		 * The <code>Effect</code> 
		 */
		private var effect:Effect;
		
		public function ValidationVisualizer()
		{	
			effect = new Glow();
			//errorEffect.duration = 1000000;
			Glow(effect).color = StyleManager.getColorName("red");
		}
		
		/**
		 * Visualize component has faild validation. 
		 * @param component the component to indicate
		 * @param validationResult <code>ValidationResultEvent</code> instance reference
		 * 
		 */
		public function visualize(component:UIComponent, validationResult:ValidationResultEvent):void 
		{
			effect.end();
			component.errorString = validationResult.message;
			component.setStyle("errorColor", StyleManager.getColorName("red"));
			if (null != component.getChildAt(0)){
				UIComponent(component.getChildAt(0)).setStyle("borderColor", StyleManager.getColorName("red"));
			}
			
			effect.play(new Array(component));
		}
		
		/**
		 * Restores component state to default state. 
		 * @param component the component to restore state of
		 */
		public function unVisualize(component:UIComponent):void
		{
			component.errorString = "";
			component.setStyle("errorColor", 0xFF0000);
			if (null != component.getChildAt(0)){
				UIComponent(component.getChildAt(0)).setStyle("borderColor", 0xB7BABC);
			}
		}
	}
}